package com.shuke.medical.insure.service;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.shuke.medical.insure.eums.ChsInfNoEnum;
import com.shuke.medical.insure.model.dto.*;
import com.shuke.medical.insure.model.vo.ChsOrderPayVO;
import com.shuke.medical.insure.model.vo.ChsPayDetailFeeVO;
import com.shuke.medical.insure.model.vo.ChsPayOrderSettleAccountsVO;
import com.shuke.medical.insure.model.vo.ChsRefundVO;
import com.shuke.medical.insure.remote.ChsPayRemoteHttp;
import com.shuke.medical.insure.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author woo hua
 * @date 2023-03-31 15:14
 */
@Service
@Slf4j
public class ChsPayService {

    @Resource
    private ChsPayRemoteHttp chsPayRemoteHttp;

    public R<ChsPayDetailFeeVO> do6201(ChsPayDetailUploadDTO dto) {
        for (ChsPayDetailFeeDTO i : dto.getFeedetailList()) {
            if (!i.getChrgBchno().equals(dto.getChrgBchno())) {
                return R.failed("【6201】异常，chrgBchno字段必须和费用明细上传的chrgBchno一致");
            }
            i.setFeeOcurTime(DateUtil.format(LocalDateTime.now(), DatePattern.NORM_DATE_PATTERN));
        }
        ChsPayDetailFeeVO vo;
        try {
            JSONObject jsonObject = chsPayRemoteHttp.doPost(ChsInfNoEnum.CHS_PAY_INF_6201
                    , JSONObject.parseObject(JSONObject.toJSONString(dto)));
            log.info("【6201】返回数据：{}", jsonObject);
            Integer code = jsonObject.getInteger("code");
            Boolean status = jsonObject.getBoolean("success");
            vo = jsonObject.getObject("data", ChsPayDetailFeeVO.class);
            if (code > 0 || !status) {
                String message = jsonObject.getString("message");
                return R.failed(message);
            }

        } catch (Exception e) {
            log.error("【6201】异常,e:" + e);
            return R.failed("【6201】异常" + e);
        }

        return R.ok(vo);
    }

    public R<ChsOrderPayVO> do6202(ChsOrderPayDTO dto) {
        ChsOrderPayVO vo;
        try {
            JSONObject jsonObject = chsPayRemoteHttp.doPost(ChsInfNoEnum.CHS_PAY_INF_6202
                    , JSONObject.parseObject(JSONObject.toJSONString(dto)));
            log.info("【6202】返回数据：{}", jsonObject);
            Integer code = jsonObject.getInteger("code");
            Boolean status = jsonObject.getBoolean("success");
            vo = jsonObject.getObject("data", ChsOrderPayVO.class);
            if (code > 0 || !status) {
                String message = jsonObject.getString("message");
                return R.failed(message);
            }
        } catch (Exception e) {
            log.error("【6202】异常,e:" + e);
            return R.failed("【6202】异常");
        }
        return R.ok(vo);
    }

    public R<ChsRefundVO> do6203(ChsRefundDTO dto) {
        ChsRefundVO vo;
        try {
            JSONObject jsonObject = chsPayRemoteHttp.doPost(ChsInfNoEnum.CHS_PAY_INF_6203
                    , JSONObject.parseObject(JSONObject.toJSONString(dto)));
            log.info("【6203】返回数据：{}", jsonObject);
            Integer code = jsonObject.getInteger("code");
            Boolean status = jsonObject.getBoolean("success");
            vo = jsonObject.getObject("data", ChsRefundVO.class);
            if (code > 0 || !status) {
                String message = jsonObject.getString("message");
                return R.failed(message);
            }
        } catch (Exception e) {
            log.error("【6203】异常,e:" + e);
            return R.failed("【6203】异常");
        }

        return R.ok(vo);
    }

    public R do6401(ChsPayDetaiQuashUploadDTO dto) {
        boolean success;
        String message;
        try {
            JSONObject jsonObject = chsPayRemoteHttp.doPost(ChsInfNoEnum.CHS_PAY_INF_6401
                    , JSONObject.parseObject(JSONObject.toJSONString(dto)));
            log.info("【6401】返回数据：{}", jsonObject);
            success = jsonObject.getBoolean("success");
            if (success) {
                return R.ok();
            }
            message = jsonObject.getString("message");
        } catch (Exception e) {
            log.error("【6401】异常,e:" + e);
            return R.failed("【6401】异常");
        }
        return R.failed(message);
    }

    public R<ChsPayOrderSettleAccountsVO> do6301(ChsPayOrderSettleAccountsDTO dto) {
        ChsPayOrderSettleAccountsVO vo;
        try {
            JSONObject jsonObject = chsPayRemoteHttp.doPost(ChsInfNoEnum.CHS_PAY_INF_6301
                    , JSONObject.parseObject(JSONObject.toJSONString(dto)));
            log.info("【6301】返回数据：{}", jsonObject);
            Integer code = jsonObject.getInteger("code");
            Boolean status = jsonObject.getBoolean("success");
            vo = jsonObject.getObject("data", ChsPayOrderSettleAccountsVO.class);
            if (code > 0 || !status) {
                String message = jsonObject.getString("message");
                return R.failed(message);
            }
        } catch (Exception e) {
            log.error("【6301】异常,e:" + e);
            return R.failed();
        }
        return R.ok(vo);
    }
}
